from django.urls import path, include
from rest_framework.routers import DefaultRouter, SimpleRouter
from rest_framework_simplejwt.views import (
    TokenObtainPairView,
    TokenRefreshView,
)

from . import views

# ==================== ViewSet路由器配置 ====================

# 默认路由器 - 包含完整的REST风格路由
default_router = DefaultRouter()
default_router.register(r'products', views.ProductModelViewSet, basename='product')
default_router.register(r'categories-readonly', views.CategoryReadOnlyViewSet, basename='category')
default_router.register(r'tags', views.CustomTagViewSet, basename='tag')
default_router.register(r'reviews', views.ReviewViewSet, basename='review')
default_router.register(r'products-bulk', views.ProductBulkViewSet, basename='product-bulk')

# 简单路由器 - 不包含API根视图
simple_router = SimpleRouter()
simple_router.register(r'simple-products', views.ProductModelViewSet, basename='simple-product')

# ==================== 应用URL配置 ====================

urlpatterns = [
    # ==================== API 根目录 ====================
    path('', views.APIRootView.as_view(), name='api-root'),

    # ==================== 认证相关URL ====================
    path('auth/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
    path('auth/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),

    # ==================== 基础APIView示例 ====================
    path('categories/basic/', views.CategoryListAPIView.as_view(), name='category-list-basic'),
    path('categories/basic/<int:pk>/', views.CategoryDetailAPIView.as_view(), name='category-detail-basic'),

    # ==================== 函数视图示例 ====================
    path('categories/function/', views.category_list_function_view, name='category-list-function'),

    # ==================== Generic Views示例 ====================
    path('products/generic/', views.ProductListCreateView.as_view(), name='product-list-create'),
    path('products/generic/<int:pk>/', views.ProductRetrieveUpdateDestroyView.as_view(), name='product-detail-generic'),

    # ==================== 自定义Mixins视图示例 ====================
    path('products/custom/', views.CustomProductView.as_view(), name='product-list-custom'),
    path('products/custom/<int:pk>/', views.CustomProductView.as_view(), name='product-detail-custom'),

    # ==================== 统计和分析API ====================
    path('products/stats/', views.ProductStatsAPIView.as_view(), name='product-stats'),

    # ==================== 图片处理API ====================
    path('products/<int:pk>/upload-image/', views.ProductImageUploadAPIView.as_view(), name='product-upload-image'),
    path('products/<int:pk>/resize-image/', views.ProductImageResizeAPIView.as_view(), name='product-resize-image'),
    path('products/<int:pk>/image-info/', views.ProductImageInfoAPIView.as_view(), name='product-image-info'),
    path('products/<int:pk>/thumbnail/', views.ProductImageThumbnailAPIView.as_view(), name='product-thumbnail'),

    # ==================== ViewSet路由 ====================
    # 使用默认路由器
    path('api/v1/', include(default_router.urls)),

    # 使用简单路由器
    path('api/simple/', include(simple_router.urls)),

    # ==================== 手动ViewSet路由示例 ====================
    # 如果不使用路由器，可以手动定义ViewSet的路由
    path('manual/products/', views.ProductModelViewSet.as_view({
        'get': 'list',
        'post': 'create'
    }), name='manual-product-list'),

    path('manual/products/<int:pk>/', views.ProductModelViewSet.as_view({
        'get': 'retrieve',
        'put': 'update',
        'patch': 'partial_update',
        'delete': 'destroy'
    }), name='manual-product-detail'),

    # ViewSet自定义action的手动路由
    path('manual/products/<int:pk>/reviews/', views.ProductModelViewSet.as_view({
        'get': 'reviews'
    }), name='manual-product-reviews'),

    path('manual/products/<int:pk>/add-review/', views.ProductModelViewSet.as_view({
        'post': 'add_review'
    }), name='manual-product-add-review'),

    path('manual/products/my/', views.ProductModelViewSet.as_view({
        'get': 'my_products'
    }), name='manual-my-products'),
]

# ==================== URL命名空间说明 ====================
"""
URL设计说明和最佳实践:

1. **基础APIView** (categories/basic/)
   - 完全手动控制，适合简单需求
   - URL: /api/categories/basic/
   - 支持: GET (列表), POST (创建)

2. **函数视图** (categories/function/)
   - 简单直接，适合简单逻辑
   - URL: /api/categories/function/
   - 支持: GET, POST

3. **Generic Views** (products/generic/)
   - 代码简洁，自动分页和过滤
   - URL: /api/products/generic/
   - 支持: 标准CRUD操作

4. **ViewSet + Router** (api/v1/)
   - 自动生成完整的RESTful路由
   - 标准REST URL模式
   - 支持自定义action

5. **手动ViewSet路由** (manual/)
   - 完全控制URL结构
   - 适合特殊需求

URL模式对比:

自动路由 (ViewSet + Router):
- GET    /api/v1/products/          -> list
- POST   /api/v1/products/          -> create
- GET    /api/v1/products/{id}/     -> retrieve
- PUT    /api/v1/products/{id}/     -> update
- PATCH  /api/v1/products/{id}/     -> partial_update
- DELETE /api/v1/products/{id}/     -> destroy
- GET    /api/v1/products/{id}/reviews/     -> custom action
- POST   /api/v1/products/{id}/add-review/  -> custom action
- GET    /api/v1/products/my/               -> custom action

手动路由:
- 可以自定义任意URL结构
- 更灵活但需要更多代码
- 适合特殊业务需求

选择建议:
1. 标准CRUD -> ViewSet + Router
2. 简单API -> Generic Views
3. 特殊逻辑 -> APIView
4. 快速原型 -> 函数视图
"""

# ==================== API版本控制示例 ====================
"""
可以通过URL前缀实现API版本控制:

# v1版本 - 当前版本
urlpatterns += [
    path('api/v1/', include('api_examples.v1_urls')),
]

# v2版本 - 未来版本
urlpatterns += [
    path('api/v2/', include('api_examples.v2_urls')),
]

或者使用DRF的版本控制:
REST_FRAMEWORK = {
    'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.URLPathVersioning',
}

然后在视图中:
def get_serializer_class(self):
    if self.request.version == 'v1':
        return V1ProductSerializer
    elif self.request.version == 'v2':
        return V2ProductSerializer
    return ProductSerializer
"""
